#include "stm32f10x.h"                  // Device header
#include "Delay.h"
//#include "OLED.h"
#include "PWM.h"
#include "Timer.h"
#include "GDM.h"  //光电门
#include "Motor.h"  //电机
#include "Encoder.h"  //编码器
#include "Serial.h"  //串口
#include "CountSensor.h"  //光耦
#include "DuoJI.h"  //舵机



/*定义结构体*/
typedef enum {
	STATE_IDLE,        //空闲状态
	STATE_ROTATING,	   //垃圾桶旋转中
	STATE_DROPPING,    //垃圾投放中
	STATE_COMPRESSING  //满载压缩中
}SystemState;

typedef enum {
	TRASH_A = 1,  //可回收
	TRASH_B = 2,  //有害
	TRASH_C = 3,  //厨余
	TRASH_D = 4   //其他
}TrashType;


volatile SystemState currentState =STATE_IDLE;
volatile TrashType targetTrashType;
//volatile uint32_t dropStartTime = 0;  // 投放开始时间
//volatile uint8_t isDropInProgress = 0;  // 投放是否在进行中


void CheckPhotocoupler(void);
void HandleFullBucket(void);

int main(void)
{
//	OLED_Init();
	Motor_Init();
	DUOJI_Init();
	Serial_Init(); //串口1
	Serial2_Init();//串口2
	Gdm_Init(); //光电门
	CountSensor_Init();  //光耦
	Timer_Init();
	
	DUOJI_SetAngle(90);
	
	while (1)
	{

		switch(currentState)
		{
			case STATE_IDLE :
				if (!isDropInProgress)  // 只有在投放未进行时检查满载
                {
                    HandleFullBucket(); // 检查满桶
                }
				break;
			case STATE_ROTATING :
				CheckPhotocoupler();//持续检测光耦
				break;
			default:
				break;
		}	
	}
}

void TIM1_UP_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
	{
		COUNT_Tick();
		
		TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
	}
}


/*投放 */
void CheckPhotocoupler(void)  //检查光耦  (第二个case)
{
	if(currentState == STATE_ROTATING)
	{
		uint16_t targetPin; 
		targetPin =  CountSensor_Get();
		if(targetPin == targetTrashType)
			{
				Motor_SetPWM(0);//停止旋转
				currentState =STATE_DROPPING;
				isDropInProgress = 1;  // 标记投放开始
				Delay_ms(1000);
				DUOJI_SetAngle(30);// 启动投放 (选合适的位置)
				Delay_ms(2800);
				DUOJI_SetAngle(90);//归位  (选合适的位置)
				currentState = STATE_IDLE;
				isDropInProgress = 0;  // 标记投放结束
			}
		else
		{
			Motor_SetPWM(20);
		}
	}
}

/* 满载*/
void HandleFullBucket(void) 
{
//	uint8_t Number;

	if(isTrashA && currentState == STATE_IDLE && !isDropInProgress)
	{
		
		
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
		{
			Motor_SetPWM(20);
		}
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==1)
		{
			
//			Serial2_SendByte(0x46);
			
			Motor_SetPWM(0);
			Delay_ms(1000);
			
			currentState=STATE_COMPRESSING;
			Motor_SetSpeed(100);
			Delay_ms(27000);
			Motor_SetSpeed(-100);
			Delay_ms(30000);
			
			isTrashA=0;
			isDropInProgress=1;
			currentState = STATE_IDLE;
			
			Serial_SendByte(0x45);
			Serial2_SendByte(0x45);
			
	
		}
	}
	
	
}


/*串口中断函数  */
void USART1_IRQHandler(void)  
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		uint8_t receivedChar =USART_ReceiveData(USART1);
		
		switch(receivedChar){
			case'A':    //可回收
				targetTrashType =TRASH_A;
				if(currentState == STATE_IDLE){
				currentState = STATE_ROTATING;
				Motor_SetPWM(250);//启动底盘旋转电机
				}
				break;
			case'B':    //有害
				targetTrashType =TRASH_B;
				if(currentState == STATE_IDLE){
				currentState = STATE_ROTATING;
				Motor_SetPWM(250);//启动底盘旋转电机
				}
				break;
			case'C':    //厨余
				targetTrashType =TRASH_C;
				if(currentState == STATE_IDLE){
				currentState = STATE_ROTATING;
				Motor_SetPWM(250);//启动底盘旋转电机
				}
				break;
			case'D':    //其他
				targetTrashType =TRASH_D;
				if(currentState == STATE_IDLE){
				currentState = STATE_ROTATING;
				Motor_SetPWM(250);//启动底盘旋转电机
				}
				break;
			default:
				break;
		}	
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}


